#include "m0wig.h"
#include "ui_m0wig.h"

M0Wig::M0Wig(QWidget *parent): QWidget(parent), ui(new Ui::M0Wig)
{
    ui->setupUi(this);
    connect(ui->pushButton_LED1, SIGNAL(clicked()), this, SLOT(btnClickedLED1Slot()));
    connect(ui->pushButton_LED2, SIGNAL(clicked()), this, SLOT(btnClickedLED2Slot()));
    connect(ui->pushButton_LED3, SIGNAL(clicked()), this, SLOT(btnClickedLED3Slot()));
    connect(ui->pushButton_Fan, SIGNAL(clicked()), this, SLOT(btnClickedFanSlot()));
    connect(ui->pushButton_Motor, SIGNAL(clicked()), this, SLOT(btnClickedMotorSlot()));
}

M0Wig::~M0Wig()
{
    delete ui;
}
/**
 * @brief boolList1 判断是否存在stm32数据
 * @param list
 * @return
 */
int boolList1(QStringList list)
{
    for(int i = 0; i < list.count(); i++)
    {
        if(!list[i].compare("stm32"))
        {
            return 1;
        }
    }
    return 0;
}
/**
 * @brief M0Wig::dataUpSlot 接收Json数据上传槽函数
 * @param Array
 */
void M0Wig::dataUpSlot(QByteArray Array)
{
    QString payload = Array;
//    qDebug() << "stm32payload" << payload;
    // 解析json的数据
    QJsonParseError jsonerror;  // 创建一个JSON解析错误对象
    QJsonDocument doc = QJsonDocument::fromJson(payload.toLatin1(), &jsonerror); // 将JSON数据解析为QJsonDocument对象
    if(!doc.isNull() && jsonerror.error == QJsonParseError::NoError)
    {
        if(doc.isObject())  // 如果是一个对象
        {
            QJsonObject objectroot = doc.object();  // 获取根节点
            qDebug() << "根节点：" << objectroot;
            QStringList listkeys = objectroot.keys();// 获取JSON对象的所有键
            qDebug() << "所有键：" << listkeys;
            int a = boolList1(listkeys);
            qDebug() << "stm32 exists:" << a;
            if(boolList1(listkeys)) // 变化上报
            {
                QJsonObject stm32obj = objectroot.value("stm32").toObject();
                QJsonArray stm32DataArray = stm32obj.value("data").toArray();
                // 使用常量迭代器遍历 QJsonArray
                for (QJsonArray::const_iterator it = stm32DataArray.begin(); it != stm32DataArray.end(); ++it)
                {
                    QJsonObject itemObj = it->toObject();
                    int key = itemObj.value("key").toInt();
                    //QString name = itemObj.value("name").toString();
                    switch(key)
                    {
                        case 301:
                            {
                                int val = itemObj.value("val").toInt();
                                ui->lineEdit_Heartbeat->setText(QString::number(val));
                            }
                            break;
                        case 302:
                            {
                                double val = itemObj.value("val").toDouble();
                                ui->lineEdit_Tem->setText(QString::number(val));
                                break;
                            }
                        case 303:
                            {
                                double val = itemObj.value("val").toDouble();

                                ui->lineEdit_Hum->setText(QString::number(val));
                                break;
                            }
                        case 304:
                            {
                                bool val = itemObj.value("val").toBool();

                                if(val == 1)
                                {
                                    ui->pushButton_LED1->setText("ON");
                                }
                                else
                                {
                                    ui->pushButton_LED1->setText("OFF");
                                }
                                break;
                            }
                        case 305:
                            {
                                bool val = itemObj.value("val").toBool();

                                if(val == 1)
                                {
                                    ui->pushButton_LED2->setText("ON");
                                }
                                else
                                {
                                    ui->pushButton_LED2->setText("OFF");
                                }
                                break;
                            }
                        case 306:
                            {
                                bool val = itemObj.value("val").toBool();

                                if(val == 1)
                                {
                                    ui->pushButton_LED3->setText("ON");
                                }
                                else
                                {
                                    ui->pushButton_LED3->setText("OFF");
                                }
                                break;
                            }
                        case 307:
                            {
                                bool val = itemObj.value("val").toBool();

                                if(val == 1)
                                {
                                    ui->pushButton_Fan->setText("ON");
                                }
                                else
                                {
                                    ui->pushButton_Fan->setText("OFF");
                                }
                                break;
                            }
                        case 308:
                            {
                                bool val = itemObj.value("val").toBool();

                                if(val == 1)
                                {
                                    ui->pushButton_Motor->setText("ON");
                                }
                                else
                                {
                                    ui->pushButton_Motor->setText("OFF");
                                }
                                break;
                            }
                        default:
                            break;
                    }
                    qDebug() << "stm32:" << key;
                }
            }
            else    // 刷新上报获取的值
            {
                qDebug() << "stm32刷新上报";
                int key = objectroot.value("key").toInt();
                switch(key)
                {
                    case 301:
                        {
                            int val = objectroot.value("val").toInt();
                            ui->lineEdit_Heartbeat->setText(QString::number(val));
                        }
                        break;
                    case 302:
                        {
                            double val = objectroot.value("val").toDouble();
                            ui->lineEdit_Tem->setText(QString::number(val));
                            break;
                        }
                    case 303:
                        {
                            double val = objectroot.value("val").toDouble();

                            ui->lineEdit_Hum->setText(QString::number(val));
                            break;
                        }
                    case 304:
                        {
                            bool val = objectroot.value("val").toBool();

                            if(val == 1)
                            {
                                ui->pushButton_LED1->setText("ON");
                            }
                            else
                            {
                                ui->pushButton_LED1->setText("OFF");
                            }
                            break;
                        }
                    case 305:
                        {
                            bool val = objectroot.value("val").toBool();

                            if(val == 1)
                            {
                                ui->pushButton_LED2->setText("ON");
                            }
                            else
                            {
                                ui->pushButton_LED2->setText("OFF");
                            }
                            break;
                        }
                    case 306:
                        {
                            bool val = objectroot.value("val").toBool();

                            if(val == 1)
                            {
                                ui->pushButton_LED3->setText("ON");
                            }
                            else
                            {
                                ui->pushButton_LED3->setText("OFF");
                            }
                            break;
                        }
                    case 307:
                        {
                            bool val = objectroot.value("val").toBool();

                            if(val == 1)
                            {
                                ui->pushButton_Fan->setText("ON");
                            }
                            else
                            {
                                ui->pushButton_Fan->setText("OFF");
                            }
                            break;
                        }
                    case 308:
                        {
                            bool val = objectroot.value("val").toBool();

                            if(val == 1)
                            {
                                ui->pushButton_Motor->setText("ON");
                            }
                            else
                            {
                                ui->pushButton_Motor->setText("OFF");
                            }
                            break;
                        }
                    default:
                        break;
                }
            }
        }
    }
}

/**
 * @brief M0Wig::btnClickedLED1Slot LED1状态设置信号槽
 */
void M0Wig::btnClickedLED1Slot()
{
    QMQTT::Message message;
    // 设置主题
    message.setTopic("down");
    static int flag = 0;
    QByteArray data;
    if(flag == 0)
    {
        data = " { \"pattern\" :1, \"key\":304,\"val\":0,\"devtype\":1} ";

        flag = 1;
        ui->pushButton_LED1->setText("OFF");
    }
    else if(flag == 1)
    {
        data = " { \"pattern\" :1, \"key\":304,\"val\":1,\"devtype\":1} ";
        flag = 0;
        ui->pushButton_LED1->setText("ON");
    }
    qDebug() << "flag" << flag;
    message.setPayload(data);
    // 发送命令
    if (GlobalVal::mqtt)
    {
        GlobalVal::mqtt->publish(message);
    }
    else
    {
        qDebug() << "MQTT client is not initialized.";
    }
}
/**
 * @brief M0Wig::btnClickedLED2Slot LED2设置信号槽
 */
void M0Wig::btnClickedLED2Slot()
{
    QMQTT::Message message;
    // 设置主题
    message.setTopic("down");
    static int flag = 0;
    QByteArray data;
    if(flag == 0)
    {
        data = " { \"pattern\" :1, \"key\":305,\"val\":0,\"devtype\":1} ";
        flag = 1;
        ui->pushButton_LED2->setText("OFF");
    }
    else if(flag == 1)
    {
        data = " { \"pattern\" :1, \"key\":305,\"val\":1,\"devtype\":1} ";
        flag = 0;
        ui->pushButton_LED2->setText("ON");
    }
    message.setPayload(data);
    // 发送命令
    if (GlobalVal::mqtt)
    {
        GlobalVal::mqtt->publish(message);
    }
    else
    {
        qDebug() << "MQTT client is not initialized.";
    }
}
/**
 * @brief M0Wig::btnClickedLED3Slot LED3设置信号槽
 */
void M0Wig::btnClickedLED3Slot()
{
    QMQTT::Message message;
    // 设置主题
    message.setTopic("down");
    static int flag = 0;
    QByteArray data;
    if(flag == 0)
    {
        data = " { \"pattern\" :1, \"key\":306,\"val\":0,\"devtype\":1} ";
        flag = 1;
        ui->pushButton_LED3->setText("OFF");
    }
    else if(flag == 1)
    {
        data = " { \"pattern\" :1, \"key\":306,\"val\":1,\"devtype\":1} ";
        flag = 0;
        ui->pushButton_LED3->setText("ON");
    }
    message.setPayload(data);
    // 发送命令
    if (GlobalVal::mqtt)
    {
        GlobalVal::mqtt->publish(message);
    }
    else
    {
        qDebug() << "MQTT client is not initialized.";
    }
}
/**
 * @brief M0Wig::btnClickedFanSlot 风扇状态设置信号槽
 */
void M0Wig::btnClickedFanSlot()
{
    QMQTT::Message message;
    // 设置主题
    message.setTopic("down");
    static int flag = 0;
    QByteArray data;
    if(flag == 0)
    {
        data = " { \"pattern\" :1, \"key\":307,\"val\":0,\"devtype\":1} ";
        flag = 1;
        ui->pushButton_Fan->setText("OFF");
    }
    else if(flag == 1)
    {
        data = " { \"pattern\" :1, \"key\":307,\"val\":1,\"devtype\":1} ";
        flag = 0;
        ui->pushButton_Fan->setText("ON");
    }
    message.setPayload(data);
    // 发送命令
    if (GlobalVal::mqtt)
    {
        GlobalVal::mqtt->publish(message);
    }
    else
    {
        qDebug() << "MQTT client is not initialized.";
    }
}
/**
 * @brief M0Wig::btnClickedMotorSlot 马达状态设置信号槽
 */
void M0Wig::btnClickedMotorSlot()
{
    QMQTT::Message message;
    // 设置主题
    message.setTopic("down");
    static int flag = 0;
    QByteArray data;
    if(flag == 0)
    {
        data = " { \"pattern\" :1, \"key\":308,\"val\":0,\"devtype\":1} ";
        flag = 1;
        ui->pushButton_Motor->setText("OFF");
    }
    else if(flag == 1)
    {
        data = " { \"pattern\" :1, \"key\":308,\"val\":1,\"devtype\":1} ";
        flag = 0;
        ui->pushButton_Motor->setText("ON");
    }
    message.setPayload(data);
    // 发送命令
    if (GlobalVal::mqtt)
    {
        GlobalVal::mqtt->publish(message);
    }
    else
    {
        qDebug() << "MQTT client is not initialized.";
    }
}
